#ifndef REQHEAD_H
#define REQHEAD_H
#include <memory.h>
#include <cstring>
//#include "Main/constant.h"

#ifdef FRAMWORKAP_PSBC  // 邮储定制的交易类报文头
#define MSGLEN		         8U
#define SEQNO                8U
#define MSGTYPE              1U
#define EXCHCODE             10U//4U
#define MSGFLAG              1U
#define TERMTYPE             2U
#define USERTYPE             2U
#define USERID              20U//10U
#define AERACODE            4U
#define BRANCHID             20U//12U
#define CTELLERID1           20U//10U
#define CTELLERID2           20U//10U
#define RSPCODE              8U
#else
#ifdef FRAMWORKAP_SIGEX		//黄金国际交易所定制的交易类报文头
#define MSGLEN		         8U
#define SEQNO                8U
#define MSGTYPE              1U
#define EXCHCODE             4U
#define MSGFLAG              1U
#define TERMTYPE             2U
#define USERTYPE             2U
#define USERID              10U
#define AERACODE            4U
#define BRANCHID             20U//12U
#define CTELLERID1           10U
#define CTELLERID2           10U
#define RSPCODE              8U
#else
#define MSGLEN		         8U
#define SEQNO                8U
#define MSGTYPE              1U
#define EXCHCODE             4U
#define MSGFLAG              1U
#define TERMTYPE             2U
#define USERTYPE             2U
#define USERID              10U
#define AERACODE            4U
#define BRANCHID             12U
#define CTELLERID1           10U
#define CTELLERID2           10U
#define RSPCODE              8U
#endif
#endif

#define LENGTH_BYTES		MSGLEN

#define MSGFLAG_OFFSET		MSGLEN+SEQNO+MSGTYPE+EXCHCODE
#define REQHEAD_BYTES		MSGLEN+SEQNO+MSGTYPE+EXCHCODE+MSGFLAG+TERMTYPE+USERTYPE+USERID+AERACODE+BRANCHID+CTELLERID1+CTELLERID2
#define RSPHEAD_BYTES		MSGLEN+SEQNO+MSGTYPE+EXCHCODE+MSGFLAG+TERMTYPE+USERTYPE+USERID+AERACODE+BRANCHID+RSPCODE

typedef struct tagReqHead   //请求报文头
{

    char msg_len[MSGLEN+1];   //报文长度
    char seq_no[SEQNO+1];    //流水号
    char msg_type[MSGTYPE+1];  //报文类型
    char exch_code[EXCHCODE+1]; //交易代码
    char msg_flag[MSGFLAG+1];  //报文标识
    char term_type[TERMTYPE+1]; //终端来源
    char user_type[USERTYPE+1]; //用户类型
    char user_id[USERID+1];   //用户ID
    char area_code[AERACODE+1]; //地区代码
    char branch_id[BRANCHID+1];  //代理机构
    char c_teller_id1[CTELLERID1+1];  //一级复核操作员
    char c_teller_id2[CTELLERID2+1];  //一级复核操作员



    std::string ToString() const
    {
        std::string sEncode = "";
        char  tempcode[128];
        char  code[1024];
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',SEQNO);
        sprintf(code,tempcode,seq_no);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',MSGTYPE);
        sprintf(code,tempcode,msg_type);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c-%ds",'%',EXCHCODE);
        sprintf(code,tempcode,exch_code);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',MSGFLAG);
        sprintf(code,tempcode,msg_flag);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',TERMTYPE);
        sprintf(code,tempcode,term_type);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c-%ds",'%',USERTYPE);
        sprintf(code,tempcode,user_type);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c-%ds",'%',USERID);
//        sprintf(tempcode,"%c%ds",'%',USERID);
        sprintf(code,tempcode,user_id);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',AERACODE);
        sprintf(code,tempcode,area_code);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',BRANCHID);
        sprintf(code,tempcode,branch_id);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',CTELLERID1);
        sprintf(code,tempcode,c_teller_id1);
        sEncode+=code;
        memset(tempcode,0x00,128);
        memset(code,0x00,1024);
        sprintf(tempcode,"%c%ds",'%',CTELLERID2);
        sprintf(code,tempcode,c_teller_id2);
        sEncode+=code;
        return sEncode;
    }
} HEADER_REQ,*PHEADER_REQ ;

typedef struct tagRspHead   //响应报文头
{
    char msg_len[MSGLEN+1];   //报文长度
    char seq_no[SEQNO+1];    //流水号
    char msg_type[MSGTYPE+1];  //报文类型
    char exch_code[EXCHCODE+1]; //交易代码
    char msg_flag[MSGFLAG+1];  //报文标识
    char term_type[TERMTYPE+1]; //终端来源
    char user_type[USERTYPE+1]; //用户类型
    char user_id[USERID+1];   //用户ID
    char area_code[AERACODE+1]; //地区代码
    char branch_id[BRANCHID+1];  //代理机构
    char rsp_code[RSPCODE+1];      //响应代码

    void DecodeRspHead( const char * pData )
    {
        const char* pPos = pData;
    #ifdef _VER_NEW_API
        pPos += 68;
    #else
        memcpy(msg_len,pPos,MSGLEN);
        pPos += MSGLEN;
        memcpy(seq_no,pPos,SEQNO);
        pPos += SEQNO;
        memcpy(msg_type,pPos,MSGTYPE);
        pPos += MSGTYPE;
        memcpy(exch_code,pPos,EXCHCODE);
        pPos += EXCHCODE;
        memcpy(msg_flag,pPos,MSGFLAG);
        pPos += MSGFLAG;
        memcpy(term_type,pPos,TERMTYPE);
        pPos += TERMTYPE;
        memcpy(user_type,pPos,USERTYPE);
        pPos += USERTYPE;
        memcpy(user_id,pPos,USERID);
        pPos += USERID;
        memcpy(area_code,pPos,AERACODE);
        pPos += AERACODE;
        memcpy(branch_id,pPos,BRANCHID);
        pPos += BRANCHID;
    #endif
        memset(rsp_code,0,RSPCODE+1);
        memcpy(rsp_code,pPos,RSPCODE);
    }

} HEADER_RSP,*PHEADER_RSP;

#endif // REQHEAD_H
